import fs from 'fs'
import config from '../config/index.js'
import { resolve } from 'dns';
const Upload = async ( ctx ) => {
  let imgData = ctx.request.body.imgData;
  if (!imgData) {
    ctx.status = 200;
    ctx.body = {
      code: -10,
      msg: '参数错误',
      userMsg: '请选择图片再进行上传操作',
    };
    return
  }
  var base64Data = imgData.replace(/^data:image\/\w+;base64,/, "");
  var dataBuffer = new Buffer(base64Data, 'base64')
  let path = config.SITE_ROOT_URL+'/static/upload/'+Date.now()+'.jpg'
  
  fs.writeFile('static/upload/'+Date.now()+'.jpg', dataBuffer, function(err) {
    if(err){
      return
    }
  })
  ctx.status = 200;
  ctx.body = {
    code: 0,
    msg: '',
    userMsg: '',
    data: {
      path: path
    }
  };
};

module.exports = {
  Upload,
};